//
//  LGRegistInputView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/5/6.
//

#import "LGRegistInputView.h"

@interface LGRegistInputView()
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIImageView *tipView;
/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , assign)NSInteger index;

@end

@implementation LGRegistInputView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
}

-(void)setType:(NSString *)type{
    _type = type;
    if([type isEqualToString:@"phone"]){
        //文字
        self.contenTF.keyboardType = UIKeyboardTypePhonePad;
        self.actionBtn.hidden = YES;
        [self.actionBtn setImage:[UIImage imageNamed:@"clearPhone"] forState:UIControlStateNormal];
        [self.actionBtn setImage:[UIImage imageNamed:@"clearPhone"] forState:UIControlStateHighlighted];
        [self.actionBtn addTarget:self action:@selector(clearContentAction) forControlEvents:UIControlEventTouchUpInside];
        
    }else if ([type isEqualToString:@"code"]){
        //验证码
        self.contenTF.keyboardType = UIKeyboardTypeNumberPad;
        self.actionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [self.actionBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.actionBtn setTitleColor:[UIColor colorWithString:@"#2E8FFF"] forState:UIControlStateNormal];
        [self.actionBtn setTitleColor:[UIColor colorWithString:@"#999999"] forState:UIControlStateSelected];
        [self.actionBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(viewPix(150)));
        }];
        
    }else if ([type isEqualToString:@"password"]){
        //密码
        self.contenTF.secureTextEntry = YES;
        self.contenTF.keyboardType = UIKeyboardTypeNamePhonePad;
        [self.actionBtn setImage:[UIImage imageNamed:@"hidenPassword"] forState:UIControlStateNormal];
        [self.actionBtn setImage:[UIImage imageNamed:@"showPassword"] forState:UIControlStateSelected];
        [self.actionBtn addTarget:self action:@selector(passwordShowAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }else if ([type isEqualToString:@"text"]){
        //文字
        self.contenTF.keyboardType = UIKeyboardTypeDefault;
        [self.actionBtn setImage:[UIImage imageNamed:@" "] forState:UIControlStateNormal];
        [self.actionBtn setImage:[UIImage imageNamed:@" "] forState:UIControlStateSelected];
        [self.actionBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(viewPix(6)));
        }];
        
    }else if ([type isEqualToString:@"select"]){
        //选项
        self.contenTF.attributedPlaceholder = [[NSMutableAttributedString alloc] init];
        self.contenTF.userInteractionEnabled = NO;
        [self.actionBtn setImage:[UIImage imageNamed:@"grayTip16"] forState:UIControlStateNormal];
        [self.actionBtn setImage:[UIImage imageNamed:@"grayTip16"] forState:UIControlStateSelected];
    }
}

//手机号-->清空输入内容
-(void)clearContentAction{
    self.contenTF.text = @"";
}

//密码-->是否显示输入内容
-(void)passwordShowAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    self.contenTF.secureTextEntry = !sender.selected;
}

//手机号-->内容显示
-(void)textFieldEditingChanged:(UITextField *)textField{
    if([self.type isEqualToString:@"phone"]){
        //手机号
        self.actionBtn.hidden = (textField.text.length == 0)?YES:NO;
        //输入手机号
        if (textField.text.length > self.index) {
            if (textField.text.length == 4 || textField.text.length == 9 ) {
                //输入
                NSMutableString * str = [[NSMutableString alloc ] initWithString:textField.text];
                [str insertString:@" " atIndex:(textField.text.length-1)];
                textField.text = str;
            }
            if (textField.text.length >= 13 ) {
                //输入完成
                textField.text = [textField.text substringToIndex:13];
            }

        }else if (textField.text.length < self.index){
            //删除
            if (textField.text.length == 4 || textField.text.length == 9) {
                textField.text = [NSString stringWithFormat:@"%@",textField.text];
                textField.text = [textField.text substringToIndex:(textField.text.length-1)];
            }
        }
        self.index = textField.text.length;
    }else{
        //去除空格
        textField.text = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
}


-(void)createSubView{
    [self addSubview:self.titleLabel];
    [self addSubview:self.contenTF];
    [self addSubview:self.actionBtn];
    [self addSubview:self.tipView];
    [self addSubview:self.lineView];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(viewPix(16));
        make.centerY.equalTo(self);
    }];
    [self.contenTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(viewPix(90));
        make.right.equalTo(self.actionBtn.mas_left).offset(-viewPix(5));
        make.centerY.equalTo(self);
    }];
    [self.actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self);
        make.right.equalTo(self).offset(-viewPix(16));
        make.width.equalTo(@(viewPix(50)));
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.actionBtn);
        make.bottom.equalTo(self);
        make.height.equalTo(@(0.5));
    }];
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UITextField *)contenTF{
    if(!_contenTF){
        _contenTF = [[UITextField alloc]init];
        _contenTF.tintColor = [UIColor colorWithString:@"#333333"];
        _contenTF.textColor = [UIColor colorWithString:@"#333333"];
        _contenTF.textAlignment = NSTextAlignmentLeft;
        _contenTF.font = LGFont(16);
        _contenTF.attributedPlaceholder = [@"请输入..." attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#DDDDDD"]}];
        [_contenTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _contenTF;
}

-(UIButton *)actionBtn{
    if(!_actionBtn){
        _actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _actionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _actionBtn.titleLabel.font = LGFontWeight(14, UIFontWeightMedium);
    }
    return _actionBtn;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor colorWithString:@"#E1E1E1"];
    }
    return _lineView;
}

@end
